import pandas as pd
import numpy as np
global null
null = np.nan


def dict_data_extract(all_data: pd.DataFrame, dictionary_field: list):
    data_new = all_data.copy()
    for new_field in dictionary_field:
        column_value = data_new[new_field]
        data_new = data_new.drop(columns=new_field)
        new_column_value = []
        for row_value in column_value:
            if pd.isna(row_value):
                new_column_value.append({})
                continue
            new_row_value = eval(row_value)
            new_column_value.append(new_row_value)
        extend_data = pd.DataFrame(new_column_value)
        extend_data = extend_data.dropna(axis='columns', how='all')
        data_new = pd.concat([data_new, extend_data], axis=1)
    return data_new


def remove_nan_column(all_data: pd.DataFrame):
    # remove nan column
    data_c = all_data.copy()
    sample_num = data_c.shape[0]
    data_c = data_c.dropna(axis='columns', how='all')
    data_cc = data_c.copy()
    for column_name, column_data in data_cc.items():
        # remove columns with too many nan
        if len(column_data[pd.isna(column_data)]) > 0.8 * sample_num:
            data_c = data_c.drop(columns=column_name)
            continue
        # remove column with only one value and nan
        # notice:in this dataset,there exist many columns with multiple value,but their nan is too many,so be deleted
        # but maybe in other dataset,some multiple values columns will remain alive,so .unique() maybe error:list can't hashable
        # at the time,please alter it manually
        if len(column_data.unique()) == 2:
            if True in pd.isna(column_data.unique()):
                data_c = data_c.drop(columns=column_name)
                continue

    return data_c


def related_product_num_calculate(raw_data: pd.DataFrame):
    # related_product:by experience,it seems that the advertise is deemed to be out,
    # so I only remain the number of related product
    data_copy = raw_data.copy()
    related_product = data_copy['related_product']
    data_copy = raw_data.drop(columns='related_product')
    new_column = []
    for value in related_product:
        if pd.isna(value):
            new_column.append(0)
            continue
        new_row = eval(value)
        if 'product_id' in new_row.keys():
            product_id = new_row['product_id']
        elif 'products' in new_row.keys():
            product_id = new_row['products'][0]['product_id']
        else:
            # no product
            new_column.append(0)
        if isinstance(product_id, str):
            new_column.append(1)
        elif pd.isna(product_id):
            new_column.append(0)
        else:
            new_column.append(len(product_id))
    data_copy['product_num'] = new_column
    return data_copy


def multiple_data_extract(raw_data: pd.DataFrame, multiple_field: list):
    data_copy = raw_data.copy()
    # multiple_field = ['status_second', 'inventory_type']
    for field in multiple_field:
        new_chart = data_copy[['promotion_id', field]]
        sample_num = new_chart.shape[0]
        data_copy = data_copy.drop(columns=field)
        # data.to_csv(r'.//data_without_name_time_multiple.csv')
        for index, row in new_chart.iterrows():
            value = row[field]
            promotion_id = row['promotion_id']
            if isinstance(value, list):
                for single_value in value:
                    new_chart = new_chart._append({"promotion_id": promotion_id, field: single_value}, ignore_index=True)
                continue
            elif isinstance(value, str):# if data is from csv file,maybe list will be a string
                try:
                    value = eval(value)
                    if isinstance(value, list):
                        for single_value in value:
                            new_chart = new_chart._append({"promotion_id": promotion_id, field: single_value}, ignore_index=True)
                    else:
                        new_chart = new_chart._append({"promotion_id": promotion_id, field: value}, ignore_index=True)
                except:
                    new_chart = new_chart._append({"promotion_id": promotion_id, field: value}, ignore_index=True)
                continue
            else:
                if pd.isna(value):
                    new_chart = new_chart._append({"promotion_id": promotion_id, field: np.nan}, ignore_index=True)
                    continue
                
                value = eval(value)
                
        new_chart = new_chart.drop(axis=0, index=range(0, sample_num))
        new_value = new_chart[field]
        new_column_name = new_value.unique()
        new_column_name = new_column_name[~pd.isna(new_column_name)]
        promotion_id = new_chart['promotion_id'].unique()
        add_chart = pd.DataFrame([], columns=new_column_name, index=promotion_id)
        for index, row in new_chart.iterrows():
            if not pd.isna(row[field]):
                add_chart.loc[row['promotion_id'], row[field]] = 1
        add_chart.index = range(0, sample_num)
        add_chart = add_chart.fillna(0)
        data_copy = pd.concat([data_copy, add_chart], axis=1)
    return data_copy


# data = pd.read_csv(r'.//ad.csv')
# sample_num = data.shape[0]
# # remove nan column
# data_ = data.dropna(axis='columns', how='all')
# # remove column with only one value and nan
# for column, c_data in data_.items():
#     temp = c_data.unique()
#     if len(temp) == 2:
#         if True in pd.isna(c_data.unique()):
#             data_ = data_.drop(columns=column)
#
# dict_field = ['optimize_goal', 'delivery_range', 'delivery_setting', 'audience', 'native_setting']
# # track_url_setting is useless,promotion materials is a set of materials,which is all link,useless
# # similarly, remove file_list and material_ids
# # in keywords,not null value's options are same,so remove it
# data_ = data_.drop(columns=['track_url_setting', 'promotion_materials', 'file_list', 'material_ids', 'keywords'])
# # optimize_goal and other dict field
# for field in dict_field:
#     temp = data_[field]
#     data_ = data_.drop(columns=field)
#     new_column = []
#     for value in temp:
#         if pd.isna(value):
#             new_column.append({})
#             continue
#         new_row = eval(value)
#         new_column.append(new_row)
#     new_dataframe = pd.DataFrame(new_column)
#     new_dataframe = new_dataframe.dropna(axis='columns', how='all')
#     data_ = pd.concat([data_, new_dataframe], axis=1)
#
# # remove columns with too many nan
# for column, c_data in data_.items():
#     if len(c_data[pd.isna(c_data)]) > 0.8*sample_num:
#         data_ = data_.drop(columns=column)
#
# data_ = related_product_num_calculate(data_)
#
# list_field = ['status_second', 'asset_ids', 'inventory_type', 'platform']
# # remove column with only one value and nan
# for column, c_data in data_.items():
#     if column in list_field:
#         c_data = pd.Series([str(i) for i in c_data])
#         c_data = pd.Series([np.nan if i == 'nan' else i for i in c_data])
#     temp = c_data.unique()
#     if len(temp) == 2:
#         if True in pd.isna(c_data.unique()):
#             data_ = data_.drop(columns=column)
# print(1)
# data_ = data_.drop(columns=['product_name', 'advertiser_name', 'name', 'project_create_time', 'project_modify_time', 'promotion_name', 'promotion_modify_time', 'promotion_create_time', 'end_time', 'schedule_time', 'start_time'])
